package http;

import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @Author: xwg
 * @CreateDate: 2023/4/9
 */


public class Ok1 {
    public static void main(String[] args) {

        OkHttpClient client =
                new OkHttpClient.Builder().build();
        Request request = new Request.Builder().url("https://gitee.com/api/v5/user?access_token=e61f2771e04bdffd174ae4118a615088")
                .get().build();
        Call call = client.newCall(request);

/**
 * 同步方法调用风格如下：内部机制 本线程耗时
* */
        try {
            Response response = call.execute();
//            正常处理函数 参数为res
            System.out.println("Success "+Thread.currentThread().getName()+ " ====");
            System.out.println(response.body().string());
        } catch (IOException e) {
//            异常处理函数 参数为e
            System.out.println("Fail");
            e.printStackTrace();
        }
/**
 * 异步方法调用风格如下：内部机制 入队 可能最终由别的线程执行 enter queue = enqueue
 * */

        call.enqueue(new Callback() {
            //   正常处理函数 参数为res
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println("Success "+Thread.currentThread().getName()+ " ====");
                System.out.println(response.body().string());
            }
            //     异常处理函数 参数为e
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("Fail");
                e.printStackTrace();
            }
        });
    }
}
